package com.xcc.cloud.nacos.controller;

import com.xcc.id.IdGenerate;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Set;
import java.util.concurrent.CopyOnWriteArraySet;

@RestController
@RefreshScope //动态刷新引用值
public class ConfigClientController {

    @Value("${config.info}")
    private String configInfo;

    @Value("${count}")
    private Integer count;


    @GetMapping("/reduce")
    public void reduce() {
        //定义变量
        int num = count;
        stack(num);
    }

    public void stack(int num) {
        System.out.println(Thread.currentThread() + "..........." + num);
        num--;
        if (num > 0) {
            stack(num);
        }
    }

    @GetMapping("/config/info")
    public String getConfigInfo() {
        return configInfo;
    }

    @GetMapping("/getId")
    public String getId() {
        Set<Long> set = new CopyOnWriteArraySet<>();
        for (int i = 0; i < 10000; i++) {
            new Thread(() -> {
                long l = IdGenerate.snowflakeId();
                set.add(l);
                System.out.println(Thread.currentThread().getName() + "\t" + l);
            }, String.valueOf(i)).start();
        }
        try {
            Thread.sleep(30000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("======================");
        System.out.println("set \t" + set.size());
        return "成功";
    }
}

